export const img2Base64 = async (source) => {
    return new Promise((resolve, reject) => {
        // 如果source是文件对象
        if (source instanceof File) {
            const reader = new FileReader();
            reader.onloadend = () => resolve(reader.result);
            reader.onerror = reject;
            reader.readAsDataURL(source);
        }
        // 如果source是字符串，则认为是图片URL
        else if (typeof source === 'string') {
            fetch(source)
                .then(response => {
                    if (!response.ok) throw new Error('网络请求失败');
                    return response.blob();
                })
                .then(blob => {
                    const reader = new FileReader();
                    reader.onloadend = () => resolve(reader.result);
                    reader.onerror = reject;
                    reader.readAsDataURL(blob);
                })
                .catch(reject);
        } else {
            reject('不支持的源类型');
        }
    });
}
